package nongchang.kuaijiecaifu.com.ncapp_android.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import butterknife.BindView;
import butterknife.ButterKnife;
import ezy.widget.view.AutofitImageView;
import nongchang.kuaijiecaifu.com.ncapp_android.R;
import nongchang.kuaijiecaifu.com.ncapp_android.common.adapter.ItemAdapter;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.good.Cart;

/**
 * Created by Administrator on 2016/5/4 0004.
 */
public class CartAdapter extends ItemAdapter<Cart, CartAdapter.CartViewHolder> {
    private Context context;

    public CartAdapter(Context context) {
        super(context);
        this.context = context;
    }

    private View.OnClickListener onAddNum;  //加商品数量接口
    private View.OnClickListener onSubNum;  //减商品数量接口
    private View.OnClickListener onDelete;  //删除数量
    private View.OnFocusChangeListener onTotalChange;
    private TextView.OnEditorActionListener onTotalTouch;

    public void setOnAddNum(View.OnClickListener onAddNum) {
        this.onAddNum = onAddNum;
    }

    public void setOnDeleteNum(View.OnClickListener onDelete) {
        this.onDelete = onDelete;
    }

    public void setOnTotalNum(View.OnFocusChangeListener onTotalNum, TextView.OnEditorActionListener onTotalTouch) {
        this.onTotalChange = onTotalNum;
        this.onTotalTouch = onTotalTouch;
    }

    public void setOnSubNum(View.OnClickListener onSubNum) {
        this.onSubNum = onSubNum;
    }


    @Override
    public CartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new CartViewHolder(inflate(R.layout.item_cart, parent));
    }

    @Override
    public void onBindViewHolder(CartViewHolder holder, int position) {
        Cart item = getItem(position);
        Glide.with(context).load(item.image).into(holder.imgCart);
        holder.txtTitle.setText(item.title);
        holder.txtOncePrice.setText("单价：￥" + item.one_price);
        holder.btnSub.setOnClickListener(onSubNum);
        holder.btnAdd.setOnClickListener(onAddNum);
        holder.btnDelete.setOnClickListener(onDelete);
        holder.btnSub.setTag(position);
        holder.btnAdd.setTag(position);
        holder.btnDelete.setTag(position);
        holder.editText.setTag(position);
        holder.editText.setOnFocusChangeListener(onTotalChange);
        holder.editText.setOnEditorActionListener(onTotalTouch);
        holder.txtTotal.setText("合计：￥" + item.total_money);
        holder.editText.setText(item.num + "");

    }

    static class CartViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_cart)
        AutofitImageView imgCart;

        @BindView(R.id.txt_title)
        TextView txtTitle;

        @BindView(R.id.txt_once_price)
        TextView txtOncePrice;

        @BindView(R.id.txt_total)
        TextView txtTotal;

        @BindView(R.id.btn_sub)
        TextView btnSub;

        @BindView(R.id.btn_add)
        TextView btnAdd;

        @BindView(R.id.edit_total)
        EditText editText;

        @BindView(R.id.btn_delete)
        ImageView btnDelete;

        public CartViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
